/*
 * ArcEmu MMORPG Server
 * Copyright (C) 2008-2010 <http://www.ArcEmu.org/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "LogonStdAfx.h"
#include "../../share/communication/CommunicationDef.h"

AuthSocket::AuthSocket(SOCKET fd) : Socket(fd, 32768, 4096)
{
	
	m_authenticated = false;
	last_recv = time(NULL);
	removedFromSet = false;
	_authSocketLock.Acquire();
	_authSockets.insert(this);
	_authSocketLock.Release();
}

AuthSocket::~AuthSocket()
{
}

void AuthSocket::OnDisconnect()
{
	if(!removedFromSet)
	{
		_authSocketLock.Acquire();
		_authSockets.erase(this);
		_authSocketLock.Release();
	}

}

typedef void (AuthSocket::*AuthHandler)(DataPacket& pack);
static AuthHandler Handlers[OPCOUNT] = {
		&AuthSocket::HandeleCamera,		// 52
};

void AuthSocket::OnRead()
{
	if(m_nRemaining == 0)
	{
		if(GetReadBuffer().GetSize() < sizeof(NetPacketHeader) )
		{
			return;
		}

		NetPacketHeader Header;
		GetReadBuffer().Read((uint8*)&Header, sizeof(NetPacketHeader) );

		Header.size = _BITSWAP16(Header.size);
		Header.cmd	= _BITSWAP16(Header.cmd);
		
		m_nRemaining = m_nSize = Header.size - sizeof(NetPacketHeader);
		m_Opcode = Header.cmd;
	}

	if(m_nRemaining > 0)
	{
		if( GetReadBuffer().GetSize() < m_nRemaining )
		{
			return;
		}
	}

	DataPacket * Packet = PACKET_NEW DataPacket ( static_cast<uint16>( m_Opcode ));

	if(m_nRemaining > 0)
	{
		GetReadBuffer().Read((uint8*)Packet->GetWriteBuffer(m_nRemaining), m_nRemaining);
	}

	m_nRemaining = m_nSize = m_Opcode = 0;


	if(m_Opcode < OPCOUNT && Handlers[m_Opcode] != NULL)
		(this->*Handlers[m_Opcode])(*Packet);

	PACKET_DELETE Packet;
}


void AuthSocket::HandeleCamera(DataPacket& pack)
{
	
}
